<template>
  <el-dialog :visible="dealVisible" width="80%" :title="dialogTitle" :before-close="closeDialog">
    <el-form :inline="true"
             :rules="rules"
             ref="form"
             :model="formData"

    >
      <el-row>

        <el-form-item label="处理类型" prop="cllx" required="required">
          <el-select v-model="formData.cllx" placeholder="请选择" clearable>
            <el-option label="人工处理-其他" value="3"></el-option>
            <el-option label="流水合并" value="4"></el-option>
          </el-select>
          <span style="color: #ff0000">
            因涉及不同发票类型负数发票开具要求不一致，请自行在相关发票类型“交易数据开票”界面处理</span>

        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="操作说明" required="required">
          <span style="color: #ff0000">
           （最多可输入128个中文字符或256个英文字符）
          </span>

        </el-form-item>
      </el-row>
      <el-row style="width: 340px">
        <el-form-item prop="czsm">
          <!--大文本框-->
          <el-input style="width: 840px" v-model="formData.czsm" type="textarea" :rows="6" placeholder="请输入内容"
                    clearable></el-input>
        </el-form-item>
      </el-row>
      <tableList
          :showIndex="false"
          :data="listDetails"
          border
          maxHeight="130"
          :heightMin="130"
          :loading="loading"
          :keySet="keySet"
          :autoHeight="false"
          :tableData="listDetails"
          :paginationShow="false"
          :pageNumber="pages.current"
          :pageSize="pages.size"
          :total="pages.total"
          :radioSelect="true"
          @radioHandleCurrentChange="radioHandleCurrentChange"
          @handleSizeChange="handleSizeChange"
          @handleCurrentChange="handleCurrentChange"
      >
      </tableList>
      <mergeDialog ref="merge" v-model="mergeShow" @resList='resList'></mergeDialog>
    </el-form>
    <el-row style="margin-top: 0px">
      <div class="centered-dialog">
        <el-button type="primary" size="mini" @click="closeDialog">关闭</el-button>
        <el-button type="primary" size="mini" @click="confirmSubmit()" :loading="loading">
          确定
        </el-button>
      </div>
    </el-row>
  </el-dialog>

</template>

<script>
import DictSelect from "@/views/invoiceTemplate/components/DictSelect/DictSelect.vue";
import tableList from "@/components/tableList/index.vue";
import mixinTable from "@/mixin/mtable";
import SelectTreeCom from "@/components/selectTree/selectTreeCom";
import {queryByRdid, toDeal} from '@/api/minusFlowManage.js'
import mergeDialog from "@/views/flowInvoice/components/mergeDialog";
import {mergeListQuery} from "@/api/transactionFlowInvoice";

export default {
  props: ['mergeShow'],
  data() {
    return {
      dealVisible: false,
      dialogTitle: '处理负数流水',
      listDetails: [],
      loading: false,
      rules: {},

      formData: {
        cllx: '',
        czsm: '',
        id: '',
        rdid: '',
        ywbh: '',
        tbdh: '',
      },
      mergeShow: false,
      localData: this.mergeShow,
      megeResult: false,
      keySet: [
        {
          label: '编号',
          englishName: 'rdid',
          width: '100px',
          hidden: true,
        },
        {
          label: '投保人',
          englishName: 'kfhm',
          width: '100px'
        },
        {
          label: '投保单号',
          englishName: 'tbdh',
          width: '150px'
        },
        {
          label: '业务编号',
          englishName: 'ywbh',
          width: '150px'
        },
        {
          label: '交易金额',
          englishName: 'jyrmbje',
          width: 100
        },

        {
          label: '税率',
          englishName: 'slv',
          width: 100
        },
        {
          label: '险种简称',
          englishName: 'xzjc',
          width: '100px'
        },
        {
          label: '交易日期',
          englishName: 'jyrq',
          width: '100px'
        },
        {
          label: '流水状态',
          englishName: 'lszt',
          width: '100px',
          formatter: (value) => {
            if (value == '1') {
              return '未开票'
            } else if (value == '2') {
              return '部分开票'
            } else if (value == '3') {
              return '已开票'
            } else if (value == '0') {
              return '已锁定'
            }
          }
        },
        {
          label: '保全项目代码',
          englishName: 'bqxmdm',
          width: '100px'
        },
        {
          label: '保全项目名称',
          englishName: 'bqxmmc',
          width: '100px'
        },
      ]
    };
  },
  components: {
    tableList,
    DictSelect,
    SelectTreeCom,
    mergeDialog,

  },
  mixins: [mixinTable],
  methods: {
    closeDialog() {
      this.dealVisible = false;
      this.listDetails = [];
      this.formData = {}
      this.$refs.form.resetFields();
    },
    //合并调用方法
    async mergeFn() {

      // 构造参数
      let param = {
        rdid: this.formData.rdid,
        allMerge: false,
        //合并方式 0：按险种代码合并，1：按税率合并
        mergeMode: "0",
        //合并查询条件 ：0：业务编号，1：投保单号
        mergeQueryCondition: "0",
        pageNo: 1,
        pageSize: 10,
      }
      this.loading = true;
      console.log(this.formData)
      const {data, msg, code} = await mergeListQuery(param)
      this.loading = false;
      if (code == 200) {
        let params = {
          id: this.formData.rdid,
          tbdh: this.formData.tbdh,
          ywbh: this.formData.ywbh,

        }
        //this.$refs.merge.mergeShow = true
        this.mergeShow = true
        this.$refs.merge.show(data, params);
      }
    },
    // 确认提交
    async confirmSubmit() {
      console.log(this.formData)
      if (this.formData.cllx === '') {
        this.$message({
          message: '请选择处理类型',
          type: 'warning'
        });
        return
      }
      //如果是合并则跳转合并页面
      if (this.formData.czsm === '') {
        this.$message({
          message: '请输入操作说明',
          type: 'warning'
        });
        return
      } else {

        if (this.formData.cllx === '4') {
          this.mergeFn()
        }else{
          this.confirmDeal()
        }

      }
    },
    //处理人工处理-其他的数据或者后续增加其他处理类型可用
    async confirmDeal() {
      this.loading = true;
      let params = {
        rdid: this.formData.rdid,
        cllx: this.formData.cllx,
        czsm: this.formData.czsm,
        id: this.formData.id,
      }
      console.log(params)
      let {data, code, message} = await toDeal(
          params
      );
      this.loading = false;
      if (code == 200) {
        this.loading = false;
        this.$message({
          message: '处理成功',
          type: 'success'
        });

        this.closeDialog();
      }
    },
    resList(data) {
      console.log(data)
      if(data)  {
        this.megeResult = data
        this.closeDialog();
        this.$parent.queryInfo();
      }
    },
    clearInfo() {
      this.$refs.form.resetFields();
    },
    //查询流水
    async getInvoice(rdid) {
      this.loading = true;
      let {data, code, message} = await queryByRdid(
          rdid
      );
      if (code == 200) {
        this.loading = false
        //返回的数据直接是个对象，直接放进数组即可，因为没有分页，所有不用拆解循环
        this.listDetails.push(data)
      }
    },
    getSelectValue(val) {
      this.formData.departmentId = val.id
    },
    radioHandleCurrentChange(val) {
      this.formData.userId = val.id
    },
    handleSizeChange(val) {
      this.pages.size = val;
    },

  },
  watch: {

  },
  mounted() {

  }
};
</script>

<style  lang = "scss" scoped>
@import '@/style/variables.scss';
.centered-dialog {
  display: flex;
  align-items: center;
  justify-content: center;
}


</style>